package com.iemes.controller.transport;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.TransportDispatchFormMap;
import com.iemes.mapper.transport.TransportDispatchMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/transportDispatch/")
@Controller
public class TransportDispatchController extends BaseController {
	@Inject
	private TransportDispatchMapper transportDispatchMapper;

	// 出厂
	@RequestMapping("/examinetransportDispatch")
	public String examineDispatch(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/transport/dispatch/transportdispatch_list";
	}

	// 客户出车序列
	@RequestMapping("/examineCustomerdispatch")
	public String Customerdispatch(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/transport/customerdispatch/customerdispatch_list";
	}

	// 客户出车顺序分页
	@ResponseBody
	@RequestMapping("/Customerdispatch_findbyPage")
	public PageView findByPage1(String pageNow, String pageSize) throws Exception {

		TransportDispatchFormMap transportDispatchFormMap = getFormMap(TransportDispatchFormMap.class);
		List<Map<String, Object>> transportDispatchFormMaps = transportDispatchMapper
				.getAllTran(transportDispatchFormMap);
		/*for (Map<String, Object> map : transportDispatchFormMaps) {
			map.put("dcid", map.get("name"));
			map.put("tcid", map.get("name"));
		}*/

		LuogicPaging(pageNow, pageSize, transportDispatchFormMaps);
		return pageView;
	}

	// 出厂分页
	@ResponseBody
	@RequestMapping("/transportDispatch_findbyPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception {
		TransportDispatchFormMap transportDispatchFormMap = getFormMap(TransportDispatchFormMap.class);

		List<Map<String, Object>> transportDispatchFormMaps = transportDispatchMapper
				.getAllInfo(transportDispatchFormMap);
		for (Map<String, Object> map : transportDispatchFormMaps) {
			map.put("outReceiptNo", map.get("outReceiptNo"));
			if (Integer.valueOf(map.get("category").toString()) == 1) {
				map.put("category", "物料编码");
			} else if (Integer.valueOf(map.get("category").toString()) == 2) {
				map.put("category", "模具编号");
			}
			map.put("test", map.get("test"));
			if (Integer.valueOf(map.get("test").toString()) == 1) {
				map.put("test", "未检验");

			} else if (Integer.valueOf(map.get("test").toString()) == 2) {
				map.put("test", "已检验");
			}

		}
		LuogicPaging(pageNow, pageSize, transportDispatchFormMaps);
		pageView.setRecords(transportDispatchFormMaps);
		return pageView;
	}

	// 出厂审批
	@RequestMapping("/transportDispatch_approval")
	public String tranup(Model model) {
		String id = getPara("id");
		List<Map<String, Object>> transportDispatchFormMaps = transportDispatchMapper.getAllInfo(null);
		// 获取对象
		if (Common.isNotEmpty(id)) {
			TransportDispatchFormMap transportDispatchFormMap = baseMapper.findbyFrist("id", id,
					TransportDispatchFormMap.class);

			for (Map<String, Object> map : transportDispatchFormMaps) {
				if (map.get("id") == transportDispatchFormMap.get("id")) {
					transportDispatchFormMap.put("driverName", map.get("driverName"));
					transportDispatchFormMap.put("name", map.get("name"));
					transportDispatchFormMap.put("licenseNo", map.get("licenseNo"));
					transportDispatchFormMap.put("fullCar", map.get("fullCar"));
					transportDispatchFormMap.put("category", map.get("category"));
					map.put("category", map.get("category"));
					if (Integer.valueOf(map.get("category").toString()) == 1) {
						transportDispatchFormMap.put("category", "物料编码");
					} else if (Integer.valueOf(map.get("category").toString()) == 2) {
						transportDispatchFormMap.put("category", "模具编号");
					}

					map.put("shifts", map.get("shifts"));
					if (Integer.valueOf(map.get("shifts").toString()) == 1) {
						transportDispatchFormMap.put("shifts", "白班");

					} else if (Integer.valueOf(map.get("shifts").toString()) == 2) {
						transportDispatchFormMap.put("shifts", "夜班");
					}
				}
			}
			model.addAttribute("transportDispatch", transportDispatchFormMap);
		}
		return Common.BACKGROUND_PATH + "/transport/dispatch/transportdispatch_approval";

	}

	// 不满车
	@RequestMapping(value = "/transportDispatch_approval1")
	public String editDispatch(Model model) {
		String id = getPara("transportDispatch.id");
		List<Map<String, Object>> transportDispatchFormMaps = transportDispatchMapper.getAllInfo(null);
		if (Common.isNotEmpty(id)) {
			TransportDispatchFormMap transportDispatchFormMap = baseMapper.findbyFrist("id", id,
					TransportDispatchFormMap.class);

			for (Map<String, Object> map : transportDispatchFormMaps) {
				if (map.get("id") == transportDispatchFormMap.get("id")) {
					transportDispatchFormMap.put("licenseNo", map.get("licenseNo"));
					transportDispatchFormMap.put("category", map.get("category"));
					transportDispatchFormMap.put("userName", map.get("userName"));
					transportDispatchFormMap.put("name", map.get("name"));
					transportDispatchFormMap.put("userName", map.get("userName"));
					transportDispatchFormMap.put("userName", map.get("userName"));
					transportDispatchFormMap.put("consent", map.get("consent"));

					map.put("category", map.get("category"));
					if (Integer.valueOf(map.get("category").toString()) == 1) {
						transportDispatchFormMap.put("category", "物料编码");
					} else if (Integer.valueOf(map.get("category").toString()) == 2) {
						transportDispatchFormMap.put("category", "模具编号");
					}

				}
			}

			model.addAttribute("transportDispatch", transportDispatchFormMap);
		}
		return Common.BACKGROUND_PATH + "/transport/dispatch/transportdispatch_dissatisfaction";
	}

	// 编辑出厂
	@RequestMapping(value = "/transportDispatch_approval2")
	public String editEntity(Model model) {
		String id = getPara("id");
		List<Map<String, Object>> transportDispatchFormMaps = transportDispatchMapper.getAllInfo(null);
		if (Common.isNotEmpty(id)) {
			TransportDispatchFormMap transportDispatchFormMap = baseMapper.findbyFrist("id", id,
					TransportDispatchFormMap.class);

			for (Map<String, Object> map : transportDispatchFormMaps) {
				if (map.get("id") == transportDispatchFormMap.get("id")) {
					transportDispatchFormMap.put("licenseNo", map.get("licenseNo"));
					transportDispatchFormMap.put("category", map.get("category"));
					transportDispatchFormMap.put("userName", map.get("userName"));
					transportDispatchFormMap.put("name", map.get("name"));
					transportDispatchFormMap.put("userName", map.get("userName"));
					transportDispatchFormMap.put("userName", map.get("userName"));
					transportDispatchFormMap.put("consent", map.get("consent"));
					transportDispatchFormMap.put("number", map.get("number"));
					transportDispatchFormMap.put("packNum", map.get("packNum"));
					transportDispatchFormMap.put("outReceiptNo", map.get("outReceiptNo"));

					map.put("category", map.get("category"));
					if (Integer.valueOf(map.get("category").toString()) == 1) {
						transportDispatchFormMap.put("category", "物料编码");
					} else if (Integer.valueOf(map.get("category").toString()) == 2) {
						transportDispatchFormMap.put("category", "模具编号");
					}

				}
			}

			model.addAttribute("transportDispatch", transportDispatchFormMap);
		}
		return Common.BACKGROUND_PATH + "/transport/dispatch/transportdispatch_edit";
	}

	@ResponseBody
	@RequestMapping(value = "transportDispatch_editEntity")
	@SystemLog(module = "车辆出厂", methods = "出厂-编辑出厂") // 凡需要处理业务逻辑的.都需要记录操作日志
	@Transactional
	public String editEntity() {
		TransportDispatchFormMap transportDispatchFormMap = getFormMap(TransportDispatchFormMap.class);
		try {
			transportDispatchMapper.editEntity(transportDispatchFormMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "fasle";
		}
		return "success";
	}

	// 删除出厂
	@ResponseBody
	@RequestMapping("transportDispatch_deleteEntity")
	@Transactional(readOnly = false) // 需要事务操作必须加入此注解
	@SystemLog(module = "删除出厂", methods = "pod按钮-删除pod按钮") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		TransportDispatchFormMap transportDispatchFormMap = getFormMap(TransportDispatchFormMap.class);
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		for (int i = 0; i < str.length; i++) {
			transportDispatchFormMap.set("status", 2);
			transportDispatchFormMap.put("id", str[i]);
			transportDispatchMapper.editEntity(transportDispatchFormMap);
		}

		return "success";
	}
}
